package com.lg.startertemplate.controller;

import com.lg.startertemplate.dao.impl.*;
import com.lg.startertemplate.domain.cascade.many2many.Course;
import com.lg.startertemplate.domain.cascade.many2many.Student;
import com.lg.startertemplate.domain.cascade.one2one.IdCard;
import com.lg.startertemplate.domain.cascade.one2many.Man;
import com.lg.startertemplate.domain.cascade.one2one.People;
import com.lg.startertemplate.domain.cascade.one2many.Dog;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Api(tags = "级联操作")
@Slf4j
public class CascadeController {

    @Autowired
    private PeopleDao peopleDao;

    @Autowired
    private ManDao manDao;

    @Autowired
    private DogDao dogDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private CourseDao courseDao;

//    ---------------------one2one-----------------------
    /**
     * 自增id 不收外界影响 ,  people.setPeopleId(111L) 不起作用
     *
     */
    @GetMapping("one2oneCreatePeople")
    public People one2oneCreatePeople(){
        IdCard idCard = new IdCard();
        idCard.setAddress("中国山东");
        People people = new People();
        people.setName("测试人员");
        people.setIdCard(idCard);
        //创建 并产生循环依赖
        idCard.setPeople(people);
        peopleDao.save(people);

        return people;
    }

    /**
     * left outer join 使用join的方式
     * 联合查询  查出两个实体结果
     * @param id
     * @return
     */
    @GetMapping("one2oneQueryPeople")
    public People one2oneQueryPeople(Long id){
        People people = peopleDao.findById(id).get();
        return people;
    }

    @GetMapping("one2oneQueryPeople2")
    public People one2oneQueryPeople2(Long id){
        return peopleDao.findById(id).get();
    }

    /**
     * 因为id都是自增的 所以CascadeController#createPeople 中的222L 是没有设置成功的
     * 先根据两个id(222L 1L) 查两个表
     * 主表存在
     * 副表不存在
     * insert副表,并生成新的自增
     * update主表,关联副表新生成的 自增id
     * @return
     */
    @GetMapping("one2oneUpdatePeople")
    public People one2oneUpdatePeople(){
        IdCard idCard = new IdCard();
        idCard.setCardId(222L);
        idCard.setAddress("中国南京");
        People people = new People();
        people.setName("修改测试人员");
        people.setIdCard(idCard);
        people.setPeopleId(1L);
        return peopleDao.save(people);
    }


    /**
     * 删除没有返回值
     *
     *  People people = new People();
     *  people.setPeopleId(id);
     *  cascadeDao.delete(people);
     *  无法级联删除,这个new 出来的这个people没有别管理
     *
     *  先查再删应该可以
     *
     */
    @GetMapping("one2oneDeletePeople")
    public void one2oneDeletePeople(Long id){
        peopleDao.deleteById(id);
    }



    //    ---------------------one2many-----------------------
    @GetMapping("one2manyCreate")
    public Man one2manyCreate(){
        Man man = new Man();
        man.setName("lxy");
        man.setAge(22);

        Dog dog = new Dog();
        dog.setName("aa");

        Dog dog2 = new Dog();
        dog2.setName("bb");

        Dog dog3 = new Dog();
        dog3.setName("cc");

        Set<Dog> dogs = new HashSet<>();
        dogs.add(dog);
        dogs.add(dog2);
        dogs.add(dog3);

        man.setDogs(dogs);

        return manDao.save(man);

    }

    @GetMapping("one2manyQuery")
    public Man one2manyQuery(Long id){
        Optional<Man> byId = manDao.findById(id);
        Man man = byId.get();
        man.setName("query11name");
        return man;
    }

    @GetMapping("one2manyQuery2")
    public Dog one2manyQuery2(Long id){
        Optional<Dog> byId = dogDao.findById(id);
        Dog dog = byId.get();
        dog.setName("query22name");
//        System.out.println( "name---> " +dog.getMan().getName());
        return dog;
    }


    @GetMapping("one2manyUpdate")
    public Man one2manyUpdate(Long id){
        Optional<Man> byId = manDao.findById(id);
        Man man = byId.get();
        man.setName("manUpdateName");
        Set<Dog> dogs = man.getDogs();
        for (Dog dog : dogs) {
            dog.setName("thisUpdateName");
        }
        return manDao.save(man);
    }


    @GetMapping("one2manyDel")
    public void one2manyDel(Long id){
        manDao.deleteById(id);
    }



    //    ---------------------many2many-----------------------
    @GetMapping("many2manyCreate")
    public Course many2manyCreate(){

        Student student1 = new Student();
        student1.setName("11");
        Student student2 = new Student();
        student2.setName("22");
        Student student3 = new Student();
        student3.setName("33");

        ArrayList<Student> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);

        Course course = new Course();
        course.setCourseName("course11");
        course.setStudents(students);

        courseDao.save(course);
        return course;
    }

    @GetMapping("many2manyUpdate")
    public Course many2manyUpdate(Long courseId){
        Optional<Course> byId = courseDao.findById(courseId);
        Course course = byId.get();

        List<Student> students = new ArrayList<>();

        Student student2 = new Student();
        student2.setStudentId(2L);

        Student student3 = new Student();
        student3.setName("33");

        students.add(student2);
        students.add(student3);

        course.setStudents(students);

        courseDao.save(course);
        return course;
    }


    @GetMapping("many2manyDel")
    public void many2manyDel(Long courseId){
        courseDao.deleteById(courseId);
    }

    @GetMapping("many2manyDel2")
    public void many2manyDel2(Long courseId){
        Course course = new Course();
        course.setCourseId(courseId);
        courseDao.delete(course);
    }

    @GetMapping("many2manyDel3")
    public void many2manyDel3(Long courseId,Long studentId){
        Course course = new Course();
        course.setCourseId(courseId);

        Student student = new Student();
        student.setStudentId(studentId);

        List<Student> students = new ArrayList<>();
        students.add(student);
        course.setStudents(students);
        courseDao.delete(course);
    }



}
